package js;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class StatsMain {


	DBUtils dbUtils = null;
	private Connection conn = null;
	private String statsDate = null;

	private static Logger log = LogManager.getLogger(StatsMain.class);

	public StatsMain() {
		dbUtils = new DBUtils();
		conn = dbUtils.getConn();
	}

	public static void main(String[] args){

		StatsMain statsMain = new StatsMain();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		statsMain.statsDate = sdf.format(new Date());

		//���������ļ���������ͳ��ʱ�䣬��ý������Բ�ͳ��
		if(!"".equals(StatsConfig.stats_date)){
			statsMain.statsDate = StatsConfig.stats_date;
			statsMain.statsAllDataCount(StatsConfig.stats_date);
		}else{
			long monitor_start_long;
			long monitor_end_long;
			String monitor_start =  statsMain.statsDate + " " + StatsConfig.monitor_start;
			String monitor_end =  statsMain.statsDate + " " + StatsConfig.monitor_end;
			long currentTime = System.currentTimeMillis();

			try {
				monitor_start_long =  statsMain.getLongTime(monitor_start);
				monitor_end_long =  statsMain.getLongTime(monitor_end);
				log.info("monitor_start_long:" + monitor_start_long +
					";monitor_end_long" + monitor_end_long + ";currentTime:" + currentTime);
				//������쵱ǰʱ���ڶ��Ž�����֮ǰ����ý������ڼ�س�פ�˿�
				if(currentTime < monitor_start_long){
					statsMain.warnForResidentPerson();
				}
				//������쵱ǰʱ���ڶ��Ž���ֹͣ����ý�������ͳ�ƶ��ŷ����������ͳ�Ʊ�
				if(currentTime > monitor_end_long){
					statsMain.statsAllDataCount(statsMain.statsDate);
				}
				//������쵱ǰʱ���ڶ��Ž����ܵĹ����У���ý������ڼ���������Ž���
				if(currentTime < monitor_end_long && currentTime > monitor_start_long){
					statsMain.warnForProgress();
				}
			} catch (ParseException e) {
				log.error("�ַ���ʱ��ת��Ϊlongʱ���쳣,���ڣ�"+ statsMain.statsDate + "������");
				//�����쳣��������ȼ�س�ס�˿ڣ�ͳ��ʧ�ܿ��պ󲹣��������Ž��̼�ؿ����ö��
				statsMain.warnForResidentPerson();
			}
		}
		statsMain.dbUtils.close();

	}

	/**
	 * ��פ�˿ڽ��̼�ؼ��澯��ÿ��һ��
	 */
	private void warnForResidentPerson(){

		String residentPersonCount = getResidentPersonCount();
		if(residentPersonCount.equals("0")){
			String content = "��פ�˿ڱ�resident_person����û�����ݣ��뼰ʱ��鴦��";
			log.info("���͸澯����:" + content);
			insertWarnSmsToBosssmSend(content);
		}else{
			insertMonitorLog("1",residentPersonCount);
		}
	}

	private void insertMonitorLog(String type, String residentPersonCount) {
		PreparedStatement pres = null;
		String sql = "insert into monitor_log(stats_type,stats_count,stats_time,operating_time) values(?,?,?,sysdate)";
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			pres.setString(1, type);
			pres.setString(2, residentPersonCount);
			pres.setString(3, statsDate);
			pres.executeUpdate();
		} catch (SQLException e) {
			log.info("��ס�˿����ݲ�������־��ʧ�ܣ����顤����,���ڣ�"+ statsDate);
			e.printStackTrace();
		} finally {
			close(pres, null);
		}

	}

	/**
	 * ���������̼�ؼ��澯��������ʱ���ڿɽ��ж��
	 */
	private void warnForProgress(){

		String lag_time = "(sysdate-" + StatsConfig.lag_time + "/60/24)";
		String getBossSmSendCount = getBossSmSendCount(lag_time);
		if(getBossSmSendCount.equals("0")){
			String content = "���ŷ��ͱ�boss_sm_send����û�����ݣ��뼰ʱ��鴦��";
			log.info("���͸澯����:" + content);
			insertWarnSmsToBosssmSend(content);
		}else{
			insertMonitorLog("1", getBossSmSendCount);
			String getCmppSbmtIsmgCount = getCmppSbmtIsmgCount(lag_time);
			if(getCmppSbmtIsmgCount.equals("0")){
				String content = "���Ž����cmpp_sbmt_ismg����û�����ݣ��뼰ʱ��鴦��";
				log.info("���͸澯����:" + content);
				insertWarnSmsToBosssmSend(content);
			}else{
				insertMonitorLog("2",getCmppSbmtIsmgCount);
			}
		}
	}

	/**
	 * ͳ�Ƹ�����������ͳ�Ʊ�����ѯչʾ
	 * @param statsDate
	 */
	private void statsAllDataCount(String statsDate){

		//ͳ�ƶ��ŷ��ͱ���������
		insertMonitorLog("1", getBossSmSendsSatsCount(statsDate));
		//ͳ�ƶ��Ž�����շ��ͳɹ���������
		insertMonitorLog("2", getCmppSbmtIsmgSuccCount(statsDate));
		//ͳ�ƶ��ŷ���ʧ�ܱ����������
		insertMonitorLog("3", getCmppSbmtIsmgfailCount(statsDate));
		//ͳ�Ƶ������Ŀ�������û��������
		insertMonitorLog("4", getHisBaseTargetusersCount(statsDate));
	}

	/**
	 * ͳ�ƶ��ŷ���ʧ�ܱ����������
	 * @param statsDate
	 * @return
	 */
	private String getCmppSbmtIsmgfailCount(String statsDate) {
		PreparedStatement pres = null;
		ResultSet rs = null;
		String getCmppSbmtIsmgfailCount = "0";
		String queryDateStr = "t.create_time>trunc(sysdate)";
		if(!"".equals(statsDate) && null !=statsDate){
			queryDateStr = "trunc(t.create_time)=to_date('"+statsDate+"','yyyyMMdd')";
		}
		String sql="select count(*) from cmpp_sbmt_ismg_fail t where " + queryDateStr;

		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				getCmppSbmtIsmgfailCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("ͳ��cmpp_sbmt_ismg_fail��ɹ������쳣,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("ͳ��cmpp_sbmt_ismg_fail��"+statsDate+"����������" + getCmppSbmtIsmgfailCount);
		return getCmppSbmtIsmgfailCount;
	}

	/**
	 * ͳ�ƶ��Ž�����շ��ͳɹ���������
	 * @param statsDate
	 * @return
	 */
	private String getCmppSbmtIsmgSuccCount(String statsDate) {
		PreparedStatement pres = null;
		ResultSet rs = null;
		String getCmppSbmtIsmgSuccCount = "0";
		String queryDateStr = "t.create_time>trunc(sysdate)";
		if(!"".equals(statsDate) && null !=statsDate){
			queryDateStr = "trunc(t.create_time)=to_date('"+statsDate+"','yyyyMMdd')";
		}
		String sql="select count(*) from cmpp_sbmt_ismg t where t.status=0 and result=0 and " + queryDateStr;

		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				getCmppSbmtIsmgSuccCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("ͳ��cmpp_sbmt_ismg��ɹ������쳣,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("ͳ��cmpp_sbmt_ismg��"+statsDate+"�ճɹ�������������" + getCmppSbmtIsmgSuccCount);
		return getCmppSbmtIsmgSuccCount;
	}

	/**
	 * ͳ�ƶ��ŷ��ͱ���������
	 * @param statsDate
	 * @return
	 */
	private String getBossSmSendsSatsCount(String statsDate) {
		PreparedStatement pres = null;
		ResultSet rs = null;
		String getBossSmSendsSatsCount = "0";
		String queryDateStr = "t.create_date>trunc(sysdate)";
		if(!"".equals(statsDate) && null !=statsDate){
			queryDateStr = "trunc(t.create_date)=to_date('"+statsDate+"','yyyyMMdd')";
		}
		String sql="select count(*) from boss_sm_send t where " + queryDateStr;

		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				getBossSmSendsSatsCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("ͳ��boss_sm_send��ɹ������쳣,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("ͳ��boss_sm_send��"+statsDate+"����������" + getBossSmSendsSatsCount);
		return getBossSmSendsSatsCount;

	}

	/**
	 * ��ѯĿ�������û���ʷ���������ݽ��룬���������ֽ�����������
	 * @param statsDate
	 * @return
	 */
	private String getHisBaseTargetusersCount(String statsDate){
		PreparedStatement pres = null;
		ResultSet rs = null;
		String baseTargetUserCount = "0";
		String queryDateStr = "t.operater_time>trunc(sysdate)";
		if(!"".equals(statsDate) && null !=statsDate){
			queryDateStr = "trunc(t.operater_time)=to_date('"+statsDate+"','yyyyMMdd')";
		}
		String sql="select count(*) from his_base_target_users t where " + queryDateStr;

		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				baseTargetUserCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("��ѯĿ�������˿ڱ��ʧ��,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("ͳ��his_base_target_users��"+statsDate+"����������" + baseTargetUserCount);
		return baseTargetUserCount;
	}

	/**
	 * ��ѯ��פ�˿ڱ��������ݽ��룬�������Ƿ�����
	 * @return
	 */
	private String getResidentPersonCount(){
		PreparedStatement pres = null;
		ResultSet rs = null;
		String residentPersonCount = "0";
		String sql="select count(*) from resident_person";
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				residentPersonCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("��ѯ��פ�˿ڱ�������ʧ��,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("ͳ��resident_person��"+statsDate+"����������" + residentPersonCount);
		return residentPersonCount;
	}

	/**
	 * ��ѯ���ŷ��ͱ��Ƿ������ݽ��룬û�оͿ����ж϶��Ž��̻�������Ŀ����̳�����
	 * @param str
	 * @return
	 */
	private String getBossSmSendCount(String str){

		PreparedStatement pres = null;
		ResultSet rs = null;
		String bossSmSendCount = "0";
		String sql="select count(*) from boss_sm_send t where t.create_date>" + str;
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				bossSmSendCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("Ԥ������ѯ���ŷ��ͱ�������ʧ�ܣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("��ѯboss_sm_send��"+statsDate+"���������ݽ��룬��������" + bossSmSendCount);
		return bossSmSendCount;
	}

	/**
	 * ��ѯ���Ž�����Ƿ������ݽ���
	 * @param str
	 * @return
	 */
	private String getCmppSbmtIsmgCount(String str){

		PreparedStatement pres = null;
		ResultSet rs = null;
		String cmppSbmtIsmgCount = "0";
		String sql="select count(*) from cmpp_sbmt_ismg t  where t.create_time>" + str;
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			if(rs.next()){
				cmppSbmtIsmgCount = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error("��ѯ���Ž����������ʧ��,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		log.info("��ѯcmpp_sbmt_ismg��"+statsDate+"���������ݽ��룬��������" + cmppSbmtIsmgCount);
		return cmppSbmtIsmgCount;
	}

	/**
	 * ���澯���Ų�����ŷ��ͱ�
	 * @param content
	 */
	private void insertWarnSmsToBosssmSend(String content){

		PreparedStatement pres = null;
		String result = "-1";
		String fetch_id = "";
		String city_id = "14";
		String sql = "insert into boss_sm_send(id,receiver,sender,content,result,create_date,modify_date,fetch_id,city_id)" +
				"values(seq_bomc_cmpp.nextval,?,?,?,?,sysdate,sysdate,?,?)";
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			conn.setAutoCommit(false);
			pres = conn.prepareStatement(sql);
			String[] str = null;
			str = StatsConfig.receiver.split(",");
			for (int i = 0; i < str.length; i++) {
				pres.setString(1, str[i]);
				pres.setString(2, StatsConfig.sender);
				pres.setString(3, content);
				pres.setString(4, result);
				pres.setString(5, fetch_id);
				pres.setString(6, city_id);
				pres.addBatch();
			}
			pres.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			log.error("����澯���������ŷ��ͱ�����쳣������");
			e.printStackTrace();
		} finally{
			close(pres, null);
		}
	}

	/**
	 * �ر�Ԥ����ͽ�����ͷ���Դ
	 */
	private void close(PreparedStatement pres,ResultSet rs){
		try {
			if(rs!=null){
				rs.close();
			}
			if(pres!=null){
				pres.close();
			}
		} catch (Exception e) {
			log.error("�رս���������쳣" + e.fillInStackTrace());
		}
	}

	/**
	 * ���ַ���ʱ��ת��Ϊlongʱ��
	 * @param strTime
	 * @return
	 * @throws ParseException
	 */
	private long getLongTime(String strTime) throws ParseException{

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.setTime(sdf.parse(strTime));
		long longTime = c.getTimeInMillis();
		return longTime;
	}

}
